﻿Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class mtCmmtHierarchy
    Inherits System.Web.UI.Page

    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtCmmtHierarchy")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Session("PlanYear") = "2557"

        If Page.IsPostBack = False Then
            ScriptText = "SetFilter();"
            PlanYearsTxt.Text = Session("PlanYear")
            Label1.Text = "ระบบจัดทำงบประมาณ >> ตั้งค่าระบบ >> Commitment Item Hierarchy"
        Else
            ScriptText = ""
        End If
    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        'If Session("users") Is Nothing Then
        '    Response.Redirect("LoginForm.aspx")
        'End If

        Dim adap As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim dt As New MasterDataSet.CmmtHierarchiesDataTable
        dt = adap.GetLevel1(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If IsPostBack = True Then
            Try
                Dim CMMDataSet As New DataSet
                GetData(WebCombo1.Value, PlanYearsTxt.Text, Val(Session("CmmtGroup1")), CMMDataSet)
                e.DataSource = CMMDataSet.Tables(0)
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
        End If
        ' MsgBox(WebCombo1.Value)
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim id As String = WebGrid1.SelectedRowKeyValue
            Dim PlanYear As Integer = PlanYearsTxt.Text
            Dim CmmGroup1 As String = Session("CmmtGroup1")
            DeleteSub(id)
            WebGrid1.ClientAction.Refresh()

            logger.Info("User " & Session("users") & " delete " & id.ToString)
        End If

    End Sub

    Sub DeleteSub(id As Decimal)
        Dim adapter As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim datatable As New MasterDataSet.CmmtHierarchiesDataTable
        Dim datatable2 As New MasterDataSet.CmmtHierarchiesDataTable
        datatable = adapter.GetDataByParentID(id, Val(PlanYearsTxt.Text), Session("CmmtGroup1"))
        For Each row As MasterDataSet.CmmtHierarchiesRow In datatable
            adapter.DeleteByID(row.Id)
            datatable = adapter.GetDataByParentID(row.Id, Val(PlanYearsTxt.Text), Session("CmmtGroup1"))
            For Each subrow As MasterDataSet.CmmtHierarchiesRow In datatable
                adapter.DeleteByID(subrow.Id)
            Next
        Next
        adapter.DeleteByID(id)
    End Sub

    Sub Plot_Grid()
        Dim CMTDataSet As New DataSet
        GetData(Val(WebCombo1.Value), PlanYearsTxt.Text, GroupTextBox.Text, CMTDataSet)
        WebGrid1.DataSource = CMTDataSet.Tables(0)
        WebGrid1.DataBind()
        Session("CmmtGroup1") = GroupTextBox.Text
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Plot_Grid()
    End Sub

    Sub GetData(root_id As Decimal, PlanYear As Decimal, CmmGroup As String, ByRef ds As DataSet)
        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("id", Type.GetType("System.Decimal"))
        dt.Columns.Add("CommitmentCode", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))
        dt.Columns.Add("ParentCode", Type.GetType("System.Decimal"))
        dt.Columns.Add("level", Type.GetType("System.Decimal"))
        Dim adapter As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim datatable As New MasterDataSet.CmmtHierarchiesDataTable

        Dim cadapter As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim Id As Decimal
        If root_id = 0 Then
            Exit Sub
        End If

        datatable = adapter.GetDataByID(root_id)

        For Each row As MasterDataSet.CmmtHierarchiesRow In datatable
            Id = row.Id
            Dim r As DataRow
            r = dt.NewRow()
            r("CommitmentCode") = If(row.IsCmmt_CmmtCodeNull, "", row.Cmmt_CmmtCode)
            r("Description") = If(row.IsDescriptionNull, "", row.Description) 'row.Cmmt_CmmtCode & "  " & cadapter.GetName(row.Cmmt_CmmtCode, PlanYear)
            'Response.Write(row.Cmmt_CmmtCode & " " & PlanYear)
            r("ParentCode") = 0
            r("id") = row.Id
            r("level") = row.Level
            dt.Rows.Add(r)
        Next


        datatable = adapter.GetDataByParentID(Id, Val(PlanYear), CmmGroup)
        For Each row As MasterDataSet.CmmtHierarchiesRow In datatable
            GetChild(row.Id, ds, dt, PlanYear, CmmGroup)
        Next
    End Sub

    Sub GetChild(ByVal parent_id As Decimal, ByRef ds As DataSet, ByRef dt As DataTable, PlanYear As Decimal, CmmGroup As String)
        Dim Madapter As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim adapter As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim datatable As New MasterDataSet.CmmtHierarchiesDataTable

        datatable = adapter.GetDataByID(parent_id)
        Dim r As DataRow
        Dim version As Integer = 1
        r = dt.NewRow()

        Dim Id As Decimal
        For Each row As MasterDataSet.CmmtHierarchiesRow In datatable

            Try
                r("CommitmentCode") = If(row.IsCmmt_CmmtCodeNull, "", row.Cmmt_CmmtCode)
                r("Description") = If(row.IsCmmt_CmmtCodeNull, "", row.Cmmt_CmmtCode) & "  " & If(row.IsDescriptionNull, Madapter.GetName(row.Cmmt_CmmtCode, PlanYear), row.Description)
                r("ParentCode") = row.ParentCmmtHierarchy_Id
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
            r("level") = row.Level
            r("id") = row.Id
            Id = row.Id
        Next
        dt.Rows.Add(r)
        datatable = adapter.GetDataByParentID(Id, PlanYear, CmmGroup)
        For Each row As MasterDataSet.CmmtHierarchiesRow In datatable

            GetChild(row.Id, ds, dt, PlanYear, CmmGroup)
        Next
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        If e.Row.Type = RowType.Record Then
            Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
            Dim level As Decimal = dr("level")
            If level = 3 Then
                Dim ImgButton As ImageButton
                ImgButton = CType(e.Row.Cells.GetNamedItem("Action").GetTemplateContainer().FindControl("ImageButton1"), Image)
                ImgButton.Visible = False
            End If
            e.Row.ExpandSelfRefRow()
        End If
    End Sub

    Protected Sub PlanYearsTxt_TextChanged(sender As Object, e As EventArgs) Handles PlanYearsTxt.TextChanged
        Dim adap As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim dt As New MasterDataSet.CmmtHierarchiesDataTable

        dt = adap.GetLevel1(Val(PlanYearsTxt.Text))
        WebCombo1.DataSource = dt
        WebCombo1.Value = Nothing

    End Sub
End Class
